using System.Collections.Generic;
using System.IO;

namespace Silvery.Data
{
    public class NTableDataWriter : NTableDataReader
    {
        public virtual void Save(Stream filestream)
        {
            StreamWriter sw = new StreamWriter(filestream);
            //char[] comma = { '\t' };
            string comma = "\t";
            List<string> _fieldlist = new List<string>();
            string line = "";
            foreach (var item in base.datadic)
            {
                foreach (string t in item.Value.Keys)
                {
                    line = line + t + comma;
                    _fieldlist.Add(t);
                }
                break;
            }
            sw.WriteLine(line);

            foreach (var item in base.datadic)
            {
                //line = item.Key + comma;
                line = "";
                foreach (var t in _fieldlist)
                {
                    var s = this[item.Key,t];
                    line = line + s + comma;                    
                }
                sw.WriteLine(line);
            }
            sw.Flush();
        }
        
        public new string this[string key, string field]
        {
            get { return base[key, field]; }
            set
            {
                if (datadic.ContainsKey(key))
                    if (datadic[key].ContainsKey(field))
                        if (datadic[key][field] != "")
                            datadic[key][field] = value;
            }
        }
    }
}
